package com.cloudsigns.factorymanager2.produce.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.BaseActivity;
import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.produce.iview.IUpdatePasswordView;
import com.cloudsigns.factorymanager2.produce.presenter.UpdatePasswordPresenter;

public class UpdatePasswordActivity extends BaseActivity implements IUpdatePasswordView {
    private RelativeLayout rl_back;
    private TextView tv_title;
    private EditText edt_old_password,edt_new_password,edt_confirm_password;
    private Button btn_save;

    private UpdatePasswordPresenter updatePasswordPresenter;

    @Override
    protected void onBaseCreate(Bundle savedInstanceState) {
        setContentView(R.layout.layout_update_password);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        rl_back=findViewById(R.id.rl_back);
        tv_title=findViewById(R.id.tv_title);
        edt_old_password=findViewById(R.id.edt_old_password);
        edt_new_password=findViewById(R.id.edt_new_password);
        edt_confirm_password=findViewById(R.id.edt_confirm_password);
        btn_save=findViewById(R.id.btn_save);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        tv_title.setText(getResources().getString(R.string.change_password));

        updatePasswordPresenter=new UpdatePasswordPresenter(this);
    }

    @Override
    public void setListener() {
        rl_back.setOnClickListener(this);
        btn_save.setOnClickListener(this);
    }

    @Override
    public void widgetClick(View view) {
switch (view.getId()){
    case R.id.rl_back:
        finish();
        break;
        // 保存
    case R.id.btn_save:
        if (TextUtils.isEmpty(edt_old_password.getText().toString())){
            showToast(getResources().getString(R.string.input_old_password));
            return;

        }else if (TextUtils.isEmpty(edt_new_password.getText().toString())){
            showToast(getResources().getString(R.string.input_new_password));
            return;

        } else if (TextUtils.isEmpty(edt_confirm_password.getText().toString())){
            showToast(getResources().getString(R.string.input_confirm_password));
            return;

        }

        else if (!edt_new_password.getText().toString().equals(edt_confirm_password.getText().toString())){
            showToast(getResources().getString(R.string.password_inconsistent));
            return;

        }else{
            updatePasswordPresenter.updatePassword();
        }
        break;
}
    }

    @Override
    public String getNpassword() {
        return edt_new_password.getText().toString();
    }

    @Override
    public String getOldPassword() {
        return edt_old_password.getText().toString();
    }

    @Override
    public void getUpdatePasswordSuccessed() {

    }

    @Override
    public void getUpdatePasswordFailed(final String desc) {
runOnUiThread(new Runnable() {
    @Override
    public void run() {
        showToast(desc);
    }
});
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
